'************************************************************************************
' Copyright  2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
'  Summary: Support for CodeDOM templates

Option Strict On
Option Explicit On

Imports System
Imports KADGen
Imports System.Diagnostics

'! Class Summary: 

Namespace KADGen.CodeGenerationSupport
   Public Class CodeDOMSupport

#Region "Class level declarations - empty"
#End Region

#Region "Constructors - empty"
#End Region

#Region "Public Methods and Properties"
      Public Shared Function GenerateViaCodeDOM( _
                  ByVal targetLanguage As CodeGenerationSupport.TargetLanguage, _
                  ByVal compileunit As CodeDom.CodeCompileUnit) _
                  As IO.Stream
         Dim provider As CodeDom.Compiler.CodeDomProvider = Nothing
         Select Case targetLanguage
            Case CodeGenerationSupport.TargetLanguage.VB
               provider = New Microsoft.VisualBasic.VBCodeProvider
            Case CodeGenerationSupport.TargetLanguage.CSharp
               provider = New Microsoft.CSharp.CSharpCodeProvider
         End Select
         If provider IsNot Nothing Then
            Return GenerateViaCodeDOM(provider, compileunit)
         End If
         Return Nothing
      End Function

      Public Shared Function GenerateViaCodeDOM( _
                  ByVal outputType As CodeGenerationSupport.OutputFileType, _
                  ByVal compileunit As CodeDom.CodeCompileUnit) _
                  As IO.Stream
         Dim provider As CodeDom.Compiler.CodeDomProvider = Nothing

         Select Case outputType
            Case CodeGenerationSupport.OutputFileType.VB
               provider = New Microsoft.VisualBasic.VBCodeProvider
            Case CodeGenerationSupport.OutputFileType.CSharp
               provider = New Microsoft.CSharp.CSharpCodeProvider
         End Select
         If provider IsNot Nothing Then
            Return GenerateViaCodeDOM(provider, compileunit)
         End If
         Return Nothing
      End Function

      Public Shared Function GenerateViaCodeDOM( _
                  ByVal provider As CodeDom.Compiler.CodeDomProvider, _
                  ByVal compileunit As CodeDom.CodeCompileUnit) _
                  As IO.Stream
         Dim tw As CodeDom.Compiler.IndentedTextWriter = Nothing
         Dim stream As IO.MemoryStream
         Dim writer As IO.StreamWriter

         Try
            stream = New IO.MemoryStream
            writer = New IO.StreamWriter(stream)
            tw = New CodeDom.Compiler.IndentedTextWriter(writer, "   ")
            provider.GenerateCodeFromCompileUnit(compileunit, tw, New CodeDom.Compiler.CodeGeneratorOptions)
            tw.Flush()
            stream.Seek(0, IO.SeekOrigin.Begin)
         Catch ex As System.Exception
            Debug.WriteLine(ex)
            If tw IsNot Nothing Then
               tw.Flush()
               tw.Close()
            End If
            ' KADLOG
            Throw
         End Try
         Return stream
      End Function

      Public Shared Sub GenerateViaCodeDOM( _
                  ByVal outputFileName As String, _
                  ByVal provider As CodeDom.Compiler.CodeDomProvider, _
                  ByVal compileunit As CodeDom.CodeCompileUnit)
         Dim tw As CodeDom.Compiler.IndentedTextWriter = Nothing

         Try
            tw = New CodeDom.Compiler.IndentedTextWriter(New IO.StreamWriter(outputFileName, False), "    ")
            provider.GenerateCodeFromCompileUnit(compileunit, tw, New CodeDom.Compiler.CodeGeneratorOptions)
         Finally
            If Not tw Is Nothing Then
               tw.Flush()
               tw.Close()
            End If
         End Try

      End Sub
#End Region

#Region "Protected and Friend Methods and Properties - empty"
#End Region

#Region "Private Methods and Properties - empty"
#End Region

   End Class
End Namespace